package com.wlts.wzy.DYUserModule.managerController;


import com.wlts.wzy.Base.BaseEntity;
import com.wlts.wzy.Base.BaseIdParam;
import com.wlts.wzy.Base.BasePageParam;
import com.wlts.wzy.DYUserModule.entity.DyOauth;
import com.wlts.wzy.DYUserModule.entity.DyStoreUser;
import com.wlts.wzy.DYUserModule.service.IDyOauthService;
import com.wlts.wzy.DYUserModule.service.IDyStoreUserService;
import com.wlts.wzy.DYUserModule.util.UserUtil;
import com.wlts.wzy.Result.Result;
import com.wlts.wzy.api.request.dy.AddDyStoreUserRequest;
import com.wlts.wzy.api.response.dy.DyOauthResponse;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import com.wlts.wzy.expand.MyPage;
import com.wlts.wzy.util.CopyUtils;
import com.wlts.wzy.util.MD5;
import com.wlts.wzy.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 * 快手授权信息 前端控制器
 * </p>
 *
 * @author zhiyong
 * @since 2021-02-01
 */
@Api(tags = "抖音商铺用户 后台管理控制器")
@RestController
@RequestMapping("/manager/dy-store-user")
public class DyStoreUserManagerController {
    @Autowired
    private IDyStoreUserService ksStoreUserService;

    @Autowired
    private IDyOauthService dyOauthService;

    @ApiOperation(value = "增加")
    @RequiresRoles("admin")
    @RequestMapping(value = "/addEntity", method = RequestMethod.POST)
    public Result<DyStoreUser> addEntity(@RequestBody AddDyStoreUserRequest request) {

        MyLambdaQueryWrapper<DyStoreUser> userWrapper = new MyLambdaQueryWrapper<>();
        userWrapper.eq(BaseEntity::getDelFlag,"0");
        userWrapper.eq(DyStoreUser::getName,request.getName());
        DyStoreUser user=ksStoreUserService.getOne(userWrapper);

        if(user!=null){
            return Result.buildResult(500,"用户名已存在！");
        }

        DyStoreUser ksStoreUser=new DyStoreUser();
        CopyUtils.copyProperties(request,ksStoreUser);
        ksStoreUser.setUuid(System.currentTimeMillis() + StringUtils.getRandomIntByLength(2));
        ksStoreUser.setPassword(MD5.md5(ksStoreUser.getPassword()));


        /**
         * 获取授权信息
         */

        MyLambdaQueryWrapper<DyOauth> queryWrapper = new MyLambdaQueryWrapper<>();

        queryWrapper.eq(DyOauth::getUserUuid, ksStoreUser.getUuid());

        DyOauth dyOauth = dyOauthService.getOne(queryWrapper);

        if (dyOauth == null) {
            dyOauth=new DyOauth();
        }

        DyOauthResponse response = UserUtil.getAccessToken(ksStoreUser.getAppKey(),ksStoreUser.getAppSecret());

        if(response==null){
            return Result.buildResult(500,"授权失败");
        }

        dyOauth.setUserUuid(ksStoreUser.getUuid());
        dyOauth.setAccessToken(response.getAccessToken());
        dyOauth.setRefreshToken(response.getRefreshToken());
        dyOauth.setExpiresIn(response.getExpiresIn());
        //  dyOauth.setOpenId(response.getOpenId());
        //access_token过期时间，单位秒，默认为172800，即48小时
        dyOauth.setTokenOverTime(LocalDateTime.now().plusSeconds(response.getExpiresIn()));
        dyOauth.setMessageStatus(0);
        dyOauth.setTokenStatus(0);


        ksStoreUser.setDySellerId(response.getShopId());

        ksStoreUser.setDyName(response.getShopName());

        ksStoreUserService.save(ksStoreUser);
        dyOauthService.saveOrUpdate(dyOauth);


        return Result.buildResult(ksStoreUser);
    }

    @ApiOperation(value = "删除")
    @RequiresRoles("admin")
    @RequestMapping(value = "/delById", method = RequestMethod.POST)
    public Result<DyStoreUser> delById(@RequestBody BaseIdParam t) {
        DyStoreUser t1 = ksStoreUserService.getById(t.getId());

        if (t1 != null) {
            t1.setDelFlag("1");
            ksStoreUserService.updateById(t1);
            return new Result<>();
        }
        return new Result<>(500,"id = " + t.getId() + " 不存在");
    }

    @ApiOperation(value = "修改")
    @RequestMapping(value = "/chaEntity", method = RequestMethod.POST)
    public Result<DyStoreUser> chaEntity(@RequestBody DyStoreUser ksStoreUser) {
        if (ksStoreUser.getNewPassword() != null) {
            ksStoreUser.setPassword(MD5.md5(ksStoreUser.getNewPassword()));
        }
        ksStoreUserService.updateById(ksStoreUser);
        return Result.buildResult(ksStoreUser);
    }

    @ApiOperation(value = "id查询")
    @RequiresRoles("admin")
    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    public Result<DyStoreUser> getById(@RequestBody BaseIdParam t) {
        return Result.buildResult(ksStoreUserService.getById(t.getId()));
    }

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    @RequiresRoles("admin")
    public Result<MyPage<DyStoreUser>> getPage(@RequestBody BasePageParam basePageParam) {

        MyLambdaQueryWrapper<DyStoreUser> userWrapper = new MyLambdaQueryWrapper<>();

        userWrapper.eq(BaseEntity::getDelFlag, "0");

        MyPage<DyStoreUser> ksStoreUserMyPage = ksStoreUserService.page(userWrapper, basePageParam.getCurrentPage(), basePageParam.getPageSize(), true);

        return Result.buildResult(ksStoreUserMyPage);
    }

}
